package com.cqeec.gams.controller;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cqeec.gams.entity.Account;
import com.cqeec.gams.service.AccountService;

@Controller
@RequestMapping("/app/user/")
public class UserController {
	@Autowired
	private AccountService accountService;

	@RequestMapping("/delete.html")
	public String delete(String id, String page, String size) {
		accountService.delete(id);
		return "redirect:/app/user/list.html?page=" + page + "&size=" + size;
	}

	@RequestMapping("/update.html")
	public ModelAndView update(Account account, String page, String size, String word) {
		ModelAndView mav = new ModelAndView("user/edit");
		mav.addObject("page", page);
		mav.addObject("size", size);
		mav.addObject("word", word);
		if (word != null && !"".equals(word)) {
			mav.addObject("where", "&name=" + word);
		}
		String accountName = account.getName();
		if (accountName == null || "".equals(accountName)) {
			mav.addObject("msgName", "请输入用户名！");
			return mav;
		}
		try {
			accountService.update(account);
			mav.addObject("message", "保存数据成功！");
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("message", "保存数据失败！");
		}
		return mav;
	}

	@RequestMapping("/save.html")
	public ModelAndView save(Account account, RedirectAttributes ra) {
		ModelAndView mav = new ModelAndView("user/add");
		String name = account.getName();
		if (name == null || "".equals(name)) {
			mav.addObject("msgName", "请输入用户名！");
			return mav;
		}
		String password=account.getPassword();
		if(password==null||"".equals(password)) {
			account.setPassword("123456");
		}
		String id = account.getId();
		if (id == null || "".equals(id)) {
			account.setId(UUID.randomUUID().toString());
		}
		try {
			accountService.insert(account);
			ra.addFlashAttribute("message", "保存数据成功！");
			mav.setViewName("redirect:/app/user/add.html");
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("message", "保存数据失败！");
		}
		return mav;
	}

	@RequestMapping("/edit.html")
	public ModelAndView gotoEditPage(String id, String page, String size, String name) {
		ModelAndView mav = new ModelAndView("user/edit");
		Account account = accountService.findById(id);
		mav.addObject("account", account);
		mav.addObject("page", page);
		mav.addObject("size", size);
		mav.addObject("word", name);
		if (name != null && !"".equals(name)) {
			mav.addObject("where", "&word=" + name);
		}
		return mav;
	}

	@RequestMapping("/add.html")
	public ModelAndView gotoAddPage() {
		ModelAndView mav = new ModelAndView("user/add");
		return mav;
	}

	@RequestMapping("/list.html")
	public ModelAndView gotoListPage(String page, String size, Account account) {
		ModelAndView mav = new ModelAndView("user/list");
		int iPage, iSize;
		iPage = page == null || "".equals(page) ? 1 : Integer.parseInt(page);
		iSize = size == null || "".equals(size) ? 2 : Integer.parseInt(size);

		String name = account.getName();
		long total;
		List<Account> list = null;
		if (name == null || "".equals(name)) {
			// 不带条件查询
			total = accountService.count();
		} else {
			// 带条件查询
			total = accountService.count(account);
			mav.addObject("where", "&name=" + account.getName());
		}

		long pages = total % iSize == 0 ? total / iSize : total / iSize + 1;
		if (pages < iPage) {
			iPage = (int) pages;
		}

		if (total != 0) {
			if (name == null || "".equals(name)) {
				// 不带条件查询
				list = accountService.findPage((iPage - 1) * iSize, iSize);
			} else {
				// 带条件查询
				list = accountService.findPage((iPage - 1) * iSize, iSize, account);
			}
		}
		mav.addObject("list", list);
		mav.addObject("page", iPage);
		mav.addObject("size", iSize);
		mav.addObject("total", total);
		mav.addObject("pages", pages);
		return mav;
	}
}
